package my.framework.ssm.sample.common.advice;

import lombok.extern.log4j.Log4j2;
import my.framework.ssm.sample.common.controller.Result;
import my.framework.ssm.sample.common.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * api 统一异常处理类
 *
 * @author NuKi
 * @date 2018/11/23 0023
 */
@Log4j2
@RestControllerAdvice(basePackages = "my.framework.ssm.sample.controller.api")
public class RestControllerExceptionAdvice {

    private void handlerError(Exception e) {
        log.error("调用接口异常", e);
        //可以添加一些别的操作，例如：发送邮件，保存日志到数据库等
    }

    @ExceptionHandler(SystemException.class)
    public Result sysError(SystemException e) {
        handlerError(e);
        return Result.error(e.getCode(), e.getMessage());
    }
}
